//
//  NovelDataBase.m
//  数据库
//
//  Created by 0.0 on 15-3-31.
//  Copyright (c) 2015年 dlou. All rights reserved.
//

#import "FavoriteDataBase.h"
#import "FavoriteModel.h"
#import "FavoriteDB.h"

@implementation FavoriteDataBase

+ (BOOL)insertWithModel:(FavoriteModel *)model
{
    
    // 第一步:编写Sql语句
    NSString *sql = [NSString stringWithFormat:@"insert into Vegetable values('%@', '%@', '%@')", model.vegetabelId, model.imageURL, model.vegetableName];
    
    // 第二步:获得数据库指针
    sqlite3 *getConnection = [FavoriteDB openDB];
    
    // 第三步:执行Sql语句
    int result = sqlite3_exec(getConnection, [sql UTF8String], nil, nil, nil); // 返回一个整形 看看是否执行成功 (往某个数据库执行sql字符串语句
    if (result == SQLITE_OK) {
        return YES;
    }
    else {
        return NO;
    }
    
}

+ (BOOL)deleteWithTable:(NSString *)vegetableId
{
    NSString *sql = [NSString stringWithFormat:@"delete from Vegetable where vegetableId = '%@' ", vegetableId];
    sqlite3 *get = [FavoriteDB openDB];
    
    int result = sqlite3_exec(get, [sql UTF8String], nil, nil, nil);

    return result;
}


+ (NSArray *)searchNovel
{

    NSString *str = [NSString stringWithFormat:@"select vegetableId, vegetableURL, vegetableName from Vegetable"];
    sqlite3 *get = [FavoriteDB openDB];
    
    // 定义数据库查询结果
    sqlite3_stmt *stmt;
    
    // 执行sql语句,并把查询到得结果赋值到stmt中
    int result = sqlite3_prepare_v2(get, [str UTF8String], -1, &stmt, nil);// 第三个参数是sql语句的长度, -1代表无限制长, 第四个参数是放到什么地方
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) { // 一行一行查找, 是否有下一行(执行一遍查询一行)
            const unsigned char *vegetableId = sqlite3_column_text(stmt, 0); // 取第0个列, 也就是第一列
            const unsigned char *vegetableURL = sqlite3_column_text(stmt, 1);
            const unsigned char *vegetableName = sqlite3_column_text(stmt, 2);
            
            NSString *vegetableId2 = [NSString stringWithUTF8String:(const char *)vegetableId]; // 将const unsigned char 类型的转换成字符串
            NSString *vegetableURL2 = [NSString stringWithUTF8String:(const char *)vegetableURL];
            NSString *vegetableName2 = [NSString stringWithUTF8String:(const char *)vegetableName];
            
            FavoriteModel *model = [FavoriteModel favoriteWithVegetableId:vegetableId2 imageURL:vegetableURL2 vegetableName:vegetableName2];
            
            [array addObject:model];
        }
    }
    
    sqlite3_finalize(stmt); // 将stmt释放掉
    
    return array;
}


+ (FavoriteModel *)searchWithId:(NSString *)vegetableId
{
    
    NSString *str = [NSString stringWithFormat:@"select * from Vegetable where vegetableId = '%@'", vegetableId];
    sqlite3 *get = [FavoriteDB openDB];
    
    // 定义数据库查询结果
    sqlite3_stmt *stmt;
    
    // 执行sql语句,并把查询到得结果赋值到stmt中
    int result = sqlite3_prepare_v2(get, [str UTF8String], -1, &stmt, nil);// 第三个参数是sql语句的长度, -1代表无限制长, 第四个参数是放到什么地方
  
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) { // 一行一行查找, 是否有下一行(执行一遍查询一行)
            const unsigned char *vegetableId = sqlite3_column_text(stmt, 0); // 取第0个列, 也就是第一列
            const unsigned char *vegetableURL = sqlite3_column_text(stmt, 1);
            const unsigned char *vegetableName = sqlite3_column_text(stmt, 2);
            
            NSString *vegetableId2 = [NSString stringWithUTF8String:(const char *)vegetableId]; // 将const unsigned char 类型的转换成字符串
            NSString *vegetableURL2 = [NSString stringWithUTF8String:(const char *)vegetableURL];
            NSString *vegetableName2 = [NSString stringWithUTF8String:(const char *)vegetableName];
            
             FavoriteModel *model = [FavoriteModel favoriteWithVegetableId:vegetableId2 imageURL:vegetableURL2 vegetableName:vegetableName2];
            
            sqlite3_finalize(stmt); // 将stmt释放掉
            
            return model;
        }
    }
    
    sqlite3_finalize(stmt); // 将stmt释放掉
    NSLog(@"%d", result);
    return nil;
}


@end
